package com.flx.noodle.websocket.dao.impl;

import com.flx.noodle.websocket.dao.RoomDao;
import com.flx.noodle.websocket.dao.mapper.RoomRowMapper;
import com.flx.noodle.websocket.entity.ChatRoom;
import com.flx.noodle.websocket.searcher.RoomSearcher;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fenglixiong on 2017/10/2.
 */
@Repository
public class RoomDaoImpl extends JdbcDaoSupport implements RoomDao{

    @Autowired
    RoomDaoImpl(@Qualifier("dataSource") DataSource dataSource){
        setDataSource(dataSource);
    }


    public ChatRoom getRoomById(int roomId) {
        JdbcTemplate _jdbc = this.getJdbcTemplate();
        String sql = "select * from ws_room where id = ?";
        List<ChatRoom> roomList = _jdbc.query(sql,new Object[]{roomId},new RoomRowMapper());
        return roomList.size()>0?roomList.get(0):null;
    }

    public int insertRoom(final ChatRoom chatRoom) {
        JdbcTemplate _jdbc = this.getJdbcTemplate();
        final String sql = "insert into ws_room (user_id,create_time) values (?,now())";
        int ret = _jdbc.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setInt(1,chatRoom.getUserId());
                return ps;
            }
        });
        return ret;
    }

    public int updateRoom(final ChatRoom chatRoom) {
        JdbcTemplate _jdbc = this.getJdbcTemplate();
        final String sql = "update ws_room set user_id=?,last_msg=?,last_time=now() where id = ? ";
        int ret = _jdbc.update(new PreparedStatementCreator() {
            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                PreparedStatement ps = connection.prepareStatement(sql);
                ps.setInt(1,chatRoom.getUserId());
                ps.setString(2,chatRoom.getLastMsg());
                ps.setInt(3,chatRoom.getChatId());
                return ps;
            }
        });
        return ret;
    }

    public int deleteRoom(int roomId) {
        JdbcTemplate _jdbc = this.getJdbcTemplate();
        String sql = "delete from ws_room where id = ?";
        int ret = _jdbc.update(sql,new Object[]{roomId});
        return ret;
    }

    public List<ChatRoom> findRoomList(RoomSearcher searcher) {
        JdbcTemplate _jdbc = this.getJdbcTemplate();
        List<Object> params = new ArrayList<Object>();
        String sql = "select * from ws_room where 1 = 1 ";
        if(searcher.getUserId()!=null){
            sql += "and user_id = ? ";
            params.add(searcher.getUserId());
        }
        if(searcher.getChatId()!=null){
            sql += "and id = ? ";
            params.add(searcher.getChatId());
        }
        if(StringUtils.isNotBlank(searcher.getLastMsg())){
            sql += "and last_msg like ? ";
            params.add("%"+searcher.getLastMsg()+"%");
        }
        String sql_count = "select count(*) from ("+sql+") s";
        int rowCount = _jdbc.queryForObject(sql_count,params.toArray(),Integer.class);
        searcher.setTotalResults(rowCount);
        sql += " order by create_time desc";
        //分页
        sql += " limit ? , ? ";
        params.add((searcher.getPage() - 1) * searcher.getPageSize());
        params.add(searcher.getPageSize());

        return _jdbc.query(sql,params.toArray(),new RoomRowMapper());
    }
}
